package com.example.springbootwebservice.config;

/**
 * @author: xulinyi
 * @create: 2019-03-25 09:55
 **/

import com.example.springbootwebservice.interceptor.AuthInterceptor;
import com.example.springbootwebservice.service.DemoService;
import com.example.springbootwebservice.service.impl.DemoServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

import javax.xml.ws.Endpoint;

@Configuration
public class CxfConfig {
    /**
     * 此方法作用是改变项目中服务名的前缀名
     * wsdl的默认访问地址为http://127.0.0.1:8080/services/*  也就是/service/*
     * 我们可以自己配置：wsdl访问地址为：http://127.0.0.1:8080/webservice/*
     * 这里我们自定义服务名为/webservice/*
     * 当然还可以直接在application.properties文件中加上cxf.path=/webservice
     * @return
     */
    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        ServletRegistrationBean bean = new ServletRegistrationBean(
                new CXFServlet(), "/webservice/*");
        bean.setLoadOnStartup(0);
        bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return bean;
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    //把实现类交给spring管理
    @Bean
    public DemoService webService() {
        return new DemoServiceImpl();
    }

    //终端路径
    @Bean
    public Endpoint endpoint() {
        //绑定要发布的服务
        EndpointImpl endpoint = new EndpointImpl(springBus(), webService());
        // 服务端添加自定义拦截器 不需要验证可以省略此方法
        endpoint.getInInterceptors().add(new AuthInterceptor());
        //绑定要发布的服务名称
        endpoint.publish("/ws");
        return endpoint;
    }
}